<!--

    Copyright © 2016-2024 The Thingsboard Authors

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<section class="name-value-map flex flex-col" [formGroup]="attributesValueFormGroup">
  <div class="flex flex-row gap-2" style="max-height: 40px; margin-top: 8px;">
    <label class="tb-title no-padding max-w-40% flex-full" style="min-width: 230px;" translate>device-profile.lwm2m.attribute-name</label>
    <label class="tb-title no-padding max-w-60% flex-full" translate>device-profile.lwm2m.attribute-value</label>
    <span [class.!hidden]="disabled" style="width: 40px;"></span>
  </div>
  <div class="map-list flex flex-col"
       *ngFor="let nameValueControl of attributesValueFormArray().controls; let $index = index"
        [formGroup]="nameValueControl">
    <div class="flex flex-row items-center justify-start gap-2">
      <mat-form-field class="max-w-40% flex-full">
        <mat-select formControlName="name" required>
          <mat-option *ngFor="let attributeName of attributeNames" [value]="attributeName"
                      [disabled]="isDisabledAttributeName(attributeName, $index)">
            {{ attributeNameTranslationMap.get(attributeName) | translate }}
          </mat-option>
        </mat-select>
        <mat-error *ngIf="nameValueControl.get('name').hasError('required')">
          {{ 'device-profile.lwm2m.attribute-name-required' | translate }}
        </mat-error>
      </mat-form-field>
      <mat-form-field class="max-w-60% flex-full">
        <input formControlName="value" matInput required type="number"
               placeholder="{{ 'key-val.value' | translate }}">
        <mat-error class="flex flex-row" *ngIf="nameValueControl.get('value').hasError('required')">
          {{ 'device-profile.lwm2m.attribute-value-required' | translate }}
        </mat-error>
        <mat-error class="flex flex-row" *ngIf="nameValueControl.get('value').hasError('min') ||
                                          nameValueControl.get('value').hasError('pattern')">
          {{ 'device-profile.lwm2m.attribute-value-pattern' | translate }}
        </mat-error>
      </mat-form-field>
      <button *ngIf="!disabled"
              mat-icon-button color="primary" style="min-width: 40px;"
              type="button"
              (click)="removeKeyVal($index)"
              matTooltip="{{ 'device-profile.lwm2m.remove-attribute' | translate }}"
              matTooltipPosition="above">
        <mat-icon>close</mat-icon>
      </button>
    </div>
  </div>
  <div [class.!hidden]="attributesValueFormArray().length"
        class="map-list flex items-center justify-center" translate>device-profile.lwm2m.no-attributes-set</div>
  <div style="margin-top: 9px;" *ngIf="!disabled && isAddEnabled">
    <button mat-stroked-button color="primary"
            [disabled]="isLoading$ | async"
            type="button"
            (click)="addKeyVal()">
      <mat-icon class="button-icon">add_circle_outline</mat-icon>
      {{ 'device-profile.lwm2m.add-attribute' | translate }}
    </button>
  </div>
</section>
